package com.woniuxy.carlife.carlifeuserweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifeuserweb.entity.Statistics;
import com.woniuxy.carlife.carlifeuserweb.entity.Storefavorites;
import com.woniuxy.carlife.carlifeuserweb.mapper.StatisticsMapper;
import com.woniuxy.carlife.carlifeuserweb.mapper.StorefavoritesMapper;
import com.woniuxy.carlife.carlifeuserweb.service.StatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zjq
 * @ClassName:StatisticsServiceImpl
 * @Description:用户数据统计
 * @date 2021/4/6 20:02
 **/

@Slf4j
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, Statistics> implements StatisticsService {


    @Resource
    StatisticsMapper statisticsMapper;

    //门店
    @Resource
    StorefavoritesMapper storefavoritesMapper;

    /*
     * 功能描述〈查询数据统计〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 14:41
     * @throws
     */

    @Override
    public Statistics findStatisticsByUserId(Integer userId) {
        // 查询条件
//        QueryWrapper<User> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("userName",user.getUserName()).eq("password",user.getPassword());
////        // 开始查询
//        User usern = userMapper.selectOne(queryWrapper);

        if (userId == 0 && ObjectUtil.isNull(userId)) {
            return null;
        }
        QueryWrapper<Statistics> statisticsQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Statistics> user_id = statisticsQueryWrapper.eq("user_id", userId);
        Statistics statistics = statisticsMapper.selectOne(statisticsQueryWrapper);
        if (ObjectUtil.isNull(statistics)) {
            return null;
        }
        return statistics;
    }

    /*
     * 功能描述〈新增数据统计用户注册时创建〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 14:42
     * @throws
     */

    @Override
    public boolean addStatisticsByUserId(Integer userId) {
        if (userId == 0 && ObjectUtil.isNull(userId)) {
            return false;
        }
        Statistics statistics = new Statistics();
        statistics.setUserId(Integer.valueOf(userId));
        log.info("开始执行新增数据统计");
        int insert = statisticsMapper.insert(statistics);
        log.info("开始执行新增数据统计完毕");
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /*
     * 功能描述〈修改收藏数增加〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 14:45
     * @throws
     */

    @Override
    public boolean modifyFavorites(Integer userId) {

        UpdateWrapper<Statistics> statisticsUpdateWrapper = new UpdateWrapper<>();
        statisticsUpdateWrapper.eq("user_id", userId);
        Statistics statistics = new Statistics();

//        查询数据统计中的收藏数量+1
        Statistics statisticsByUserId = findStatisticsByUserId(userId);
        Integer favoritesSumnumber = statisticsByUserId.getFavoritesSumnumber() + 1;

        statistics.setFavoritesSumnumber(favoritesSumnumber);
        log.info("开始执行修改收藏数");
        int update = statisticsMapper.update(statistics, statisticsUpdateWrapper);
        log.info("开始执行修改收藏数完毕");
        if (update > 0) {
            return true;
        }
        return false;
    }

    /*
     * 功能描述〈修改收藏数减少〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 14:45
     * @throws
     */

    @Override
    public boolean reduceFavorites(Integer userId) {
        UpdateWrapper<Statistics> statisticsUpdateWrapper = new UpdateWrapper<>();
        statisticsUpdateWrapper.eq("user_id", userId);
        Statistics statistics = new Statistics();

//        查询数据统计中的收藏数量-1
        Statistics statisticsByUserId = findStatisticsByUserId(userId);
        Integer favoritesSumnumber = statisticsByUserId.getFavoritesSumnumber() - 1;

        statistics.setFavoritesSumnumber(favoritesSumnumber);
        log.info("开始执行修改收藏数");
        int update = statisticsMapper.update(statistics, statisticsUpdateWrapper);
        log.info("开始执行修改收藏数完毕");
        if (update > 0) {
            return true;
        }

        return false;
    }

    /*
     * 功能描述〈修改购物车数增加〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 14:45
     * @throws
     */

    @Override
    public boolean modifyShoppingCart(Integer userId) {
        UpdateWrapper<Statistics> statisticsUpdateWrapper = new UpdateWrapper<>();
        statisticsUpdateWrapper.eq("user_id", userId);
        Statistics statistics = new Statistics();

//        查询数据统计中的购物车数量+1
        Statistics statisticsByUserId = findStatisticsByUserId(userId);
        Integer shoppingcartSumnumber = statisticsByUserId.getShoppingcartSumnumber() + 1;

        statistics.setShoppingcartSumnumber(shoppingcartSumnumber);
        log.info("开始执行修改购物车数");
        int update = statisticsMapper.update(statistics, statisticsUpdateWrapper);
        log.info("开始执行修改购物车数");
        if (update > 0) {
            return true;
        }
        return false;
    }

    /*
     * 功能描述〈修改购物车数减少〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 14:45
     * @throws
     */

    @Override
    public boolean reduceShoppingCart(Integer userId) {
        UpdateWrapper<Statistics> statisticsUpdateWrapper = new UpdateWrapper<>();
        statisticsUpdateWrapper.eq("user_id", userId);
        Statistics statistics = new Statistics();

//        查询数据统计中的购物车数量+1
        Statistics statisticsByUserId = findStatisticsByUserId(userId);
        Integer shoppingcartSumnumber = statisticsByUserId.getShoppingcartSumnumber() - 1;

        statistics.setShoppingcartSumnumber(shoppingcartSumnumber);
        log.info("开始执行修改购物车数");
        int update = statisticsMapper.update(statistics, statisticsUpdateWrapper);
        log.info("开始执行修改购物车数");
        if (update > 0) {
            return true;
        }
        return false;
    }

    /*
     * 功能描述〈修改优惠卷数，调取优惠卷接口〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 14:45
     * @throws
     */

    @Override
    public boolean modifyCoupon(Integer userId) {
        return false;
    }


    /*
     * 功能描述〈统计多少用户收藏门店〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/16 14:32
     * @throws
     */
    @Override
    public Integer totalNumberOfStoreCollections(Integer storeId) {
        QueryWrapper<Storefavorites> storefavoritesQueryWrapper = new QueryWrapper<>();
        storefavoritesQueryWrapper.eq("store_id", storeId);
        Integer integer1 = storefavoritesMapper.selectCount(storefavoritesQueryWrapper);
        return integer1;
    }
}
